<?php

use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel;
use Fisharebest\Webtrees\Http\RequestHandlers\ModulesAllPage;
use Fisharebest\Webtrees\I18N;

?>

<?= view('components/breadcrumbs', ['links' => [route(ControlPanel::class) => I18N::translate('Control panel'), route(ModulesAllPage::class) => I18N::translate('Modules'), $title]]) ?>

<h1><?= $title ?></h1>

<form method="post" class="form-inline mb-4">
    <?= csrf_field() ?>

    <label for="tree" class="sr-only">
        <?= I18N::translate('Family tree') ?>
    </label>

    <?= view('components/select', ['name' => 'tree', 'selected' => $tree->name(), 'options' => $tree_names]) ?>
    <button type="submit" class="btn btn-primary">
        <?= I18N::translate('show') ?>
    </button>
</form>

<p>
    <a href="<?= e(route('module', ['module' => 'stories', 'action' => 'AdminEdit', 'tree' => $tree->name()])) ?>" class="btn btn-link">
        <?= view('icons/add') ?>
        <?= I18N::translate('Add a story') ?>
    </a>
</p>

<table class="table table-bordered table-sm">
    <thead>
        <tr>
            <th><?= I18N::translate('Individual') ?></th>
            <th><?= I18N::translate('Story title') ?></th>
            <th><?= I18N::translate('Edit') ?></th>
            <th><?= I18N::translate('Delete') ?></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($stories as $story) : ?>
            <tr>
                <td>
                    <?php if ($story->individual !== null) : ?>
                        <a href="<?= e($story->individual->url()) ?>#tab-stories">
                            <?= $story->individual->fullName() ?>
                        </a>
                    <?php else : ?>
                        <?= $story->xref ?>
                    <?php endif ?>
                </td>
                <td>
                    <?= e($story->title) ?>
                </td>
                <td>
                    <a class="btn btn-primary" href="<?= e(route('module', ['module' => 'stories', 'action' => 'AdminEdit', 'tree' => $tree->name(), 'block_id' => $story->block_id])) ?>">
                        <?= view('icons/edit') ?>
                        <?= I18N::translate('Edit') ?>
                    </a>
                </td>
                <td>
                    <form method="post" action="<?= e(route('module', ['module' => 'stories', 'action' => 'AdminDelete', 'tree' => $tree->name(), 'block_id' => $story->block_id])) ?>">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-danger" data-confirm="<?= I18N::translate('Are you sure you want to delete “%s”?', e($story->title)) ?>" onclick="return confirm(this.dataset.confirm);">
                            <?= view('icons/delete') ?>
                            <?= I18N::translate('Delete') ?>
                        </button>
                    </form>
                </td>
            </tr>
        <?php endforeach ?>
    </tbody>
</table>
